<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>添加用户</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/all.min.css">
    <style>
        .container {
            padding: 20px;
            max-width: 600px;
            margin: 0 auto;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            color: #333;
            font-weight: bold;
        }
        .form-control {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .btn-group {
            margin-top: 20px;
        }
        .btn {
            padding: 8px 15px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        .btn-primary {
            background-color: #1ab394;
            color: white;
        }
        .btn-primary:hover {
            background-color: #18a689;
        }
        .btn-default {
            background-color: #f3f3f4;
            color: #333;
        }
        .btn-default:hover {
            background-color: #e7eaec;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2><i class="fas fa-plus-circle"></i> 添加用户</h2>
        <form id="userForm" action="${pageContext.request.contextPath}/admin/user/add" method="post">
            <div class="form-group">
                <label for="username">用户名：</label>
                <input type="text" id="username" name="username" class="form-control" required>
            </div>
            
            <div class="form-group">
                <label for="password">密码：</label>
                <input type="password" id="password" name="password" class="form-control" required>
            </div>
            
            <div class="form-group">
                <label for="roleId">角色：</label>
                <select id="roleId" name="roleId" class="form-control" required>
                    <option value="">请选择角色</option>
                    <option value="1">管理员</option>
                    <option value="2">学生</option>
                    <option value="3">教师</option>
                </select>
            </div>
            
            <div class="form-group">
                <label for="realName">真实姓名：</label>
                <input type="text" id="realName" name="realName" class="form-control">
            </div>
            
            <div class="form-group">
                <label for="email">邮箱：</label>
                <input type="email" id="email" name="email" class="form-control">
            </div>
            
            <div class="btn-group">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-save"></i> 保存
                </button>
                <button type="button" class="btn btn-default" onclick="history.back()">
                    <i class="fas fa-arrow-left"></i> 返回
                </button>
            </div>
        </form>
    </div>

    <script>
        document.getElementById('userForm').onsubmit = function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();
            const roleId = document.getElementById('roleId').value.trim();
            
            if (!username) {
                alert('请输入用户名');
                return;
            }
            if (!password) {
                alert('请输入密码');
                return;
            }
            if (!roleId) {
                alert('请选择角色');
                return;
            }
            
            const formData = new FormData(this);
            fetch(this.action, {
                method: 'POST',
                body: new URLSearchParams(formData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('添加成功');
                    window.location.href = '${pageContext.request.contextPath}/admin/user/list';
                } else {
                    alert(data.message || '添加失败');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('添加失败');
            });
        };
    </script>
</body>
</html> 